<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <style>
        .aui-bar-nav {
            background-color: #FFF;
            color: #000;
        }

        .aui-bar-nav .aui-iconfont {
            color: #000!important;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <header id="header" class="aui-bar aui-bar-nav">
        <a class="aui-pull-left aui-btn" tapmode onclick="comeBack()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">绑定手机号</div>
    </header>
    <div class="aui-content money_ti bind_account">
        <div class="bottom-box">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">手机号</div>
                        <div class="aui-list-item-input">
                            <input id="phone" type="number" placeholder="请输入手机号">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item bind_phone_li">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">验证码</div>
                        <div class="aui-list-item-input">
                            <input type="text" id="code" placeholder="请输入验证码">
                            <button type="button" onclick="getCode(this)" class="getcode">获取验证码</button>
                        </div>
                    </div>
                </li>
            </ul>

        </div>

        <div class="bottom-btn"><button onclick="bindPhone();" id="loginBtn" disabled type="button">绑定</button></div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../script/md5.min.js"></script>
<script type="text/javascript">
    var userInfo;
    var countdown = 10;  //
    apiready = function() {
        initHeard();
        userInfo = $api.getStorage('user');
        console.log(userInfo)
        userInfo = JSON.parse(userInfo)
        if (userInfo.mobile) {
            $("#phone").val(userInfo.mobile);
        }
    };
    function bindPhone(){
      var phone = $("#phone").val();
      var isMob = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
      var code = $("#code").val();
       if(phone == ''){
         _msg("请输入手机号")
         return false;
       } else if(!isMob.test(phone)) {
           _msg("手机号输入有误");
         return false;
       }else if (code == '') {
         _msg("请输入验证码")
         return false;
       }
       _ajax("user/bindMobile",{mobile:phone,code:code},function(res,err) {
         console.log(JSON.stringify(res))
          if(res){
            _msg(res.msg);
            if(res.errCode == 1){
              userInfo.mobile = phone;
              $api.setStorage('user', JSON.stringify(userInfo));
                setTimeout(function(){
                  comeBack();
                },800)
            }else if (res.errCode == -1) {
                openWin('login');
            }
          }
       })

    };
    //验证码倒计时
    function getCode(that){
         var phone = $("#phone").val();
         var isMob = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
          if(phone == ''){
            _msg("请输入手机号")
            return false;
          } else if(!isMob.test(phone)) {
              _msg("手机号输入有误");
            return false;
          }
          _ajax("index/send",{mobile:phone},function(res,err) {
             if(res){
               _msg(res.msg);
               if(res.errCode != 0){
                    that.setAttribute("disabled", true);
                    settime(that);
                    $("#loginBtn").attr("disabled", false);
               }else {
                 that.setAttribute("disabled", true);
               }
             }
          })
    };
    function settime(that) {
      if (countdown == 0) {
        that.removeAttribute("disabled");
        that.innerText = "重新发送";
        countdown = 10;
        return false;
      } else {
        that.innerText = "剩余" + countdown + "s";
        countdown--;
        setTimeout(function() {
          settime(that)
        }, 1000)
      }
    };
</script>

</html>
